2 // This file is part of the LWES .NET Binding (LWES.net)
4 // COPYRIGHT© 2009, Phillip Clark (cerebralkungfu[at*g mail[dot*com)
5 // original .NET implementation
7 // LWES.net is free software: you can redistribute it and/or modify
8 // it under the terms of the Lesser GNU General Public License as published by
9 // the Free Software Foundation, either version 3 of the License, or
10 // (at your option) any later version.
12 // LWES.net is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // Lesser GNU General Public License for more details.
17 // You should have received a copy of the Lesser GNU General Public License
18 // along with LWES.net. If not, see <http://www.gnu.org/licenses/>.
20 namespace Org
.Lwes
.Tests
23 using System
.Collections
.Generic
;
26 using System
.Threading
;
29 using Org
.Lwes
.Listener
;
35 static void Main(string[] args
)
37 using (IEventListener listener
= EventListener
.CreateDefault())
39 EventSink sink
= new EventSink();
40 listener
.RegisterEventSink(sink
).Activate();
41 ManualResetEvent printerExited
= new ManualResetEvent(false);
42 bool userChoseToExit
= false;
43 int receivedEvents
= 0;
45 Console
.WriteLine("LWES EventListener -\r\n This console will continue to queue and print LWES events until\r\n the user types 'exit' followed by a carriage return.");
47 Object consoleWriteLock
= new Object();
49 ThreadPool
.QueueUserWorkItem(new WaitCallback((s
) =>
52 while (!userChoseToExit
)
54 if (sink
.Events
.TryDequeue(out ev
))
56 lock (consoleWriteLock
)
58 Console
.WriteLine(ev
.ToString(true));
70 ThreadPool
.QueueUserWorkItem(new WaitCallback((s
) =>
72 while (!userChoseToExit
)
74 int eventCount
= receivedEvents
;
76 if (eventCount
< receivedEvents
)
78 lock (consoleWriteLock
)
80 Console
.WriteLine("LWES EventListener -\r\n This console will continue to queue and print LWES events until\r\n the user types 'exit' followed by a carriage return.");
89 input
= Console
.ReadLine();
90 } while (!String
.Equals(input
, "exit", StringComparison
.CurrentCultureIgnoreCase
));
91 userChoseToExit
= true;
94 printerExited
.WaitOne();
102 class EventSink
: IEventSink
106 SimpleLockFreeQueue
<Event
> _incomingEvents
= new SimpleLockFreeQueue
<Event
>();
112 public SimpleLockFreeQueue
<Event
> Events
114 get { return _incomingEvents; }
117 public bool IsThreadSafe
119 get { return false; }
122 #endregion Properties
126 public void HandleEventArrival(IEventSinkRegistrationKey key
, Event ev
)
128 _incomingEvents
.Enqueue(ev
);
131 public GarbageHandlingVote
HandleGarbageData(IEventSinkRegistrationKey key
, System
.Net
.EndPoint remoteEndPoint
, int priorGarbageCountForEndpoint
, byte[] garbage
)
133 return GarbageHandlingVote
.IgnoreAllTrafficFromEndpoint
;
139 #endregion Nested Types